package com.naihe.anderson.sms.application;

import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Locale;

import android.app.Application;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.naihe.anderson.sms.broadcastReceiver.DeliveryFilterSmsReceiver;
import com.naihe.anderson.sms.broadcastReceiver.SendFilterSmsReceiver;
import com.naihe.anderson.sms.broadcastReceiver.ServiceBootReceiver;
import com.naihe.anderson.sms.manager.ContactMgr;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.manager.NoteMgr;
import com.naihe.anderson.sms.manager.NotificationMgr;
import com.naihe.anderson.sms.manager.SignatureMsgMgr;
import com.naihe.anderson.sms.util.FileUtil;

public class NhSmsApp extends Application {
	
	NhSmsMgr nhSmsMgr;
	NoteMgr noteMgr;
	ContactMgr contactMgr;
	SignatureMsgMgr signatureMsgMgr;
	NotificationMgr notificationMgr;
	
	public static boolean DEBUG_MODE = true;
	private SendFilterSmsReceiver sendReceiver;  
    private DeliveryFilterSmsReceiver deliveryReceiver;  
    
	public static RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(Locale.CHINA);
	
	@Override
	public void onCreate() {
		super.onCreate();
		PhoneCrashHandler.getInstance().init(this); 
	    
		FileUtil.addLog("NhSmsApp::onCreate", NhSmsApp.class, "NhSmsApp", null);
		getContactMgr().init();
		// Changes the Priority of the calling Thread!
		android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
		// Changes the Priority of passed Thread (first param)
		android.os.Process.setThreadPriority(android.os.Process.myTid(), android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
		
		// 注册send  
        sendReceiver = new SendFilterSmsReceiver();
        IntentFilter sendFilter = new IntentFilter(SendFilterSmsReceiver.ACTION_SMS_SEND);  
        registerReceiver(sendReceiver, sendFilter);  
        // 注册delivery  
        deliveryReceiver = new DeliveryFilterSmsReceiver();
        IntentFilter deliveryFilter = new IntentFilter(DeliveryFilterSmsReceiver.ACTION_SMS_DELIVERY);  
        registerReceiver(deliveryReceiver, deliveryFilter);  
	}

	public SharedPreferences getDefaultSharedPreferences(){
		return PreferenceManager.getDefaultSharedPreferences(this);
	}
	
//	public SharedPreferences getSharedPreferences(){
//		return  getSharedPreferences("" + "_com.naihe.anderson.sms_preferences", Context.MODE_PRIVATE);
//	}
	
	public NhSmsMgr getNhSmsMgr() {
		if(nhSmsMgr == null){
			nhSmsMgr = new NhSmsMgr(this);
		}
		return nhSmsMgr;
	}
	
	public NoteMgr getNoteMgr() {
		if(noteMgr == null){
			noteMgr = new NoteMgr(this);
		}
		return noteMgr;
	}
	
	public NotificationMgr getNotificationMgr() {
		if(notificationMgr == null){
			notificationMgr = new NotificationMgr(this);
		}
		return notificationMgr;
	}
	
	public SignatureMsgMgr getSignatureMsgMgr() {
		if(signatureMsgMgr == null){
			signatureMsgMgr = new SignatureMsgMgr(this);
		}
		return signatureMsgMgr;
	}
	
	public void bootStar(){
		FileUtil.addLog("ServiceBootReceiver::onReceive", ServiceBootReceiver.class, "ServiceBootReceiver", null);
	}

	public ContactMgr getContactMgr() {
		if(contactMgr == null){
			contactMgr = new ContactMgr(this);
		}
		return contactMgr;
	}
	
	@Override
	protected void finalize() throws Throwable {
		
		super.finalize();
	}
}
